package com.ebocy.hammer.battle.fight;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.ebocy.hammer.battle.fight.message.C2SAttackMessage;
import com.ebocy.hammer.battle.fight.message.C2SBreakBlockMessage;
import com.ebocy.hammer.battle.fight.message.C2SBrickMessage;
import com.ebocy.hammer.battle.fight.message.C2SBufferStopMessage;
import com.ebocy.hammer.battle.fight.message.C2SClimbLadderMessage;
import com.ebocy.hammer.battle.fight.message.C2SDizzyMessage;
import com.ebocy.hammer.battle.fight.message.C2SEnterRoomMessage;
import com.ebocy.hammer.battle.fight.message.C2SFallMessage;
import com.ebocy.hammer.battle.fight.message.C2SFallingMessage;
import com.ebocy.hammer.battle.fight.message.C2SItemFallMessage;
import com.ebocy.hammer.battle.fight.message.C2SMoveMessage;
import com.ebocy.hammer.battle.fight.message.C2SPrepareCombatMessage;
import com.ebocy.hammer.battle.fight.message.C2SSafeLandMessage;
import com.ebocy.hammer.battle.fight.message.C2SSafeLandingMessage;
import com.ebocy.hammer.battle.fight.message.C2SSwitchNomalMessage;
import com.ebocy.hammer.battle.fight.message.C2STouchItemMessage;
import com.ebocy.hammer.battle.fight.message.C2SUseItemMessage;
import com.ebocy.hammer.battle.fight.message.C2SWormUpAttackMessage;
import com.ebocy.hammer.battle.fight.message.S2CCombatLeaveNotifyMessage;
import com.ebocy.hammer.battle.fight.service.FightService;
import com.ebocy.hammer.core.GameCache;
import com.ebocy.hammer.core.constants.GameMessageCode;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.domain.Room;
import com.ebocy.hammer.core.handler.IMessageHandler;
import com.ebocy.hammer.core.log.EbocyLogManager;
import com.ebocy.hammer.core.message.IMessage;
import com.ebocy.hammer.core.service.GamePlayerService;

public class FightHandler implements IMessageHandler {
	private static Logger logger = EbocyLogManager.getSystemLogger();
	private static short[] messageCodes = { GameMessageCode.HERT_BEAT, GameMessageCode.LOGOUT, GameMessageCode.BATTLE_MOVE, GameMessageCode.BATTLE_WORMUP, GameMessageCode.ENTER_ROOM, GameMessageCode.BATTLE_ATTACK, GameMessageCode.BATTLE_WORMUP_ATTACK_AFTER, GameMessageCode.BATTLE_DIZZY, GameMessageCode.BATTLE_ITEM_ATTACK, GameMessageCode.BATTLE_SAFE_LANDING, GameMessageCode.BATTLE_SAFE_LAND, GameMessageCode.BATTLE_FALLING, GameMessageCode.BATTLE_FALL, GameMessageCode.BATTLE_CLIMB_LADDER, GameMessageCode.BATTLE_BRICK, GameMessageCode.BATTLE_SWITCH_NOMAL, GameMessageCode.BATTLE_USE_ITEM, GameMessageCode.BATTLE_TOUCH_ITEM, GameMessageCode.BATTLE_WORMUP_ATTACK, GameMessageCode.BATTLE_STOP_BUFFER, GameMessageCode.BATTLE_PREPARE_COMBAT, GameMessageCode.BATTLE_CONTINUE_NOTIFY,
			GameMessageCode.BATTLE_LEAVE_NOTIFY, GameMessageCode.BATTLE_BREAK_BLOCK, GameMessageCode.BATTLE_HOME_PAGE_IP, GameMessageCode.BATTLE_ITEM_FALL, GameMessageCode.RESOURCE_INIT, };

	private static GamePlayerService gamePlayerService = GamePlayerService.getInstance();

	// 饿汉式单例
	private FightService fightService = FightService.getInstance();
	private static FightHandler fightHandler = null;

	private FightHandler() {
	}

	public static FightHandler getInstance() {
		if (fightHandler == null) {
			fightHandler = new FightHandler();
		}
		return fightHandler;
	}

	@Override
	public short[] getMessageCodes() {
		return messageCodes;
	}

	@Override
	public void resolve(IMessage message, IoSession session) {
		GamePlayer gamePlayer = GameCache.getInstance().getGamePlayerBySession(session);
		switch (message.getMessageCode()) {
		case GameMessageCode.HERT_BEAT:
			if (gamePlayer != null) {
				gamePlayer.heartBeatTimestamp = System.currentTimeMillis();
				
			}
			break;
		case GameMessageCode.LOGOUT:
			gamePlayerService.exitGame(gamePlayer);
			if (gamePlayer != null) {
				Room room = GameCache.getRoom(gamePlayer.getRoomId());
				if (room != null) {
					if (room.state == 1)
						fightService.battleStart(gamePlayer);
				}
			}
			break;
		case GameMessageCode.ENTER_ROOM:
			fightService.enterRoom(((C2SEnterRoomMessage) message).getUserId(), gamePlayer, session);
			break;
		case GameMessageCode.BATTLE_MOVE:
			C2SMoveMessage moveMessage = (C2SMoveMessage) message;
			fightService.move(gamePlayer, moveMessage.getX(), moveMessage.getY(), moveMessage.getDir());
			break;
		case GameMessageCode.BATTLE_WORMUP:
			fightService.wormUp(gamePlayer);
			break;
		case GameMessageCode.BATTLE_ATTACK:
			C2SAttackMessage attackMessage = (C2SAttackMessage) message;
			fightService.attack(gamePlayer, attackMessage.dir);
			break;
		case GameMessageCode.BATTLE_WORMUP_ATTACK_AFTER:
			// C2SAfterWormUpMessage wormUpAttackMessage =
			// (C2SAfterWormUpMessage)message;
			// fightService.wormUpAttack(gamePlayer, wormUpAttackMessage.getX(),
			// wormUpAttackMessage.getY(), wormUpAttackMessage.getDir(),
			// wormUpAttackMessage.getEndurance());
			fightService.afterWormUpAttack(gamePlayer);
			break;
		case GameMessageCode.BATTLE_DIZZY:
			C2SDizzyMessage dizzyMessage = (C2SDizzyMessage) message;
			fightService.dizzy(gamePlayer, dizzyMessage.getX(), dizzyMessage.getY());
			break;
		case GameMessageCode.BATTLE_SAFE_LANDING:
			C2SSafeLandingMessage landingMessage = (C2SSafeLandingMessage) message;
			fightService.safeLanding(gamePlayer, landingMessage.getX(), landingMessage.getY());
			break;
		case GameMessageCode.BATTLE_SAFE_LAND:
			C2SSafeLandMessage landMessage = (C2SSafeLandMessage) message;
			fightService.safeLand(gamePlayer, landMessage.getX(), landMessage.getY());
			break;
		case GameMessageCode.BATTLE_FALLING:
			C2SFallingMessage fallingMessage = (C2SFallingMessage) message;
			fightService.falling(gamePlayer, fallingMessage.getX(), fallingMessage.getY());
			break;
		case GameMessageCode.BATTLE_FALL:
			C2SFallMessage fallMessage = (C2SFallMessage) message;
			fightService.fall(gamePlayer, fallMessage.getX(), fallMessage.getY());
			break;
		case GameMessageCode.BATTLE_CLIMB_LADDER:
			C2SClimbLadderMessage climbLadderMessage = (C2SClimbLadderMessage) message;
			fightService.climbLadder(gamePlayer, climbLadderMessage.getX(), climbLadderMessage.getY());
			break;
		case GameMessageCode.BATTLE_BRICK:
			C2SBrickMessage brickMessage = (C2SBrickMessage) message;
			fightService.brick(gamePlayer, brickMessage.getX(), brickMessage.getY(), brickMessage.getEndurance());
			break;
		case GameMessageCode.BATTLE_SWITCH_NOMAL:
			C2SSwitchNomalMessage switchNomalMessage = (C2SSwitchNomalMessage) message;
			fightService.switchNomalStatus(switchNomalMessage.getX(), switchNomalMessage.getY(), gamePlayer);
			break;
		case GameMessageCode.BATTLE_USE_ITEM:
			C2SUseItemMessage useItemMessage = (C2SUseItemMessage) message;
			fightService.useItem(gamePlayer, useItemMessage.itemId, useItemMessage.x, useItemMessage.y);
			break;
		case GameMessageCode.BATTLE_TOUCH_ITEM:
			C2STouchItemMessage touchItemMessage = (C2STouchItemMessage) message;
			fightService.touchItem(gamePlayer, touchItemMessage.uuid, touchItemMessage.itemId, touchItemMessage.x, touchItemMessage.y);
			break;
		case GameMessageCode.BATTLE_WORMUP_ATTACK:
			C2SWormUpAttackMessage wormUpAttackMessage = (C2SWormUpAttackMessage) message;
			fightService.wormUpAttack(gamePlayer, wormUpAttackMessage.getDir());
			break;
		case GameMessageCode.BATTLE_STOP_BUFFER:
			C2SBufferStopMessage bufferRemoveMessage = (C2SBufferStopMessage) message;
			fightService.bufferRemove(gamePlayer, bufferRemoveMessage.bufferId);
			break;
		case GameMessageCode.BATTLE_PREPARE_COMBAT:
			C2SPrepareCombatMessage prepareMessage = (C2SPrepareCombatMessage) message;
			gamePlayer = gamePlayerService.getGamePlayerById(prepareMessage.getGamePlayerId());
			gamePlayer.heartBeatTimestamp=System.currentTimeMillis();
			gamePlayer.setSession(session);
			GameCache.getInstance().addGamePlayer(gamePlayer);
			fightService.prepare4V4Combat(gamePlayer, prepareMessage.getRoomId(), prepareMessage.getMapName());
			break;
		case GameMessageCode.RESOURCE_INIT:
			fightService.battleStart(gamePlayer);
			break;
		case GameMessageCode.BATTLE_CONTINUE_NOTIFY:

			break;
		case GameMessageCode.BATTLE_LEAVE_NOTIFY:
			S2CCombatLeaveNotifyMessage leaveMessage = new S2CCombatLeaveNotifyMessage(gamePlayer.getGamePlayerId());
			gamePlayer.sendMessageToRoom(leaveMessage);
			break;
		case GameMessageCode.BATTLE_GOT:

			break;
		case GameMessageCode.BATTLE_BREAK_BLOCK:
			C2SBreakBlockMessage breakBlockMessage = (C2SBreakBlockMessage) message;
			fightService.breakBlock(gamePlayer, breakBlockMessage.row, breakBlockMessage.column, breakBlockMessage.deadPlayerId);
			break;
		case GameMessageCode.BATTLE_HOME_PAGE_IP:
			fightService.combatLeave(gamePlayer);
			break;
		case GameMessageCode.BATTLE_ITEM_FALL:
			C2SItemFallMessage itemFallMessage = (C2SItemFallMessage) message;
			fightService.itemFall(gamePlayer, itemFallMessage.itemUUID, itemFallMessage.x, itemFallMessage.y);
			break;
		default:
			logger.error("code" + message.getMessageCode() + "未处理！");
		}
	}
}